<?php

declare(strict_types=1);

namespace App\Model\Act;

use Hyperf\DbConnection\Model\Model;

/**
 *  主播任务奖励
 */
class HostTaskReward extends Model
{
    protected $table = 'act_host_task';
    protected $fillable = ['user_id', 'task_type', 'status', 'reward_coin'];

    //任务已完成，奖励待领取
    const STATUS_ING = 0;
    //任务已完成，奖励已领取
    const STATUS_DONE = 1;

    /**
     * 新增任务记录
     */
    static function addRecord($userId = 0, $tasktype = '', $rewardCoin = 0)
    {
        return self::updateOrCreate([
            'user_id' => $userId,
            'task_type' => $tasktype
        ], [
            'reward_coin' => $rewardCoin,
        ]);
    }

    /**
     * 领取金币
     */
    static function updateDone($userId = 0, $tasktype = '', $rewardCoin = 0)
    {
        return self::updateOrCreate([
            'user_id' => $userId,
            'task_type' => $tasktype
        ], [
            'reward_coin' => $rewardCoin,
            'status' => self::STATUS_DONE
        ]);
    }

    static function getHostTask($userId = 0)
    {
        $where = ['user_id' => $userId];
        return self::where($where)->pluck('status', 'task_type')->toArray();
    }

    static function checkDone($userId = 0, $tasktype = '')
    {
        $where = ['user_id' => $userId, 'task_type' => $tasktype, 'status' => self::STATUS_DONE];
        return self::where($where)->count();
    }
}
